/*
	Copyright (c) 2010 <copyright holders>
	This source code is licensed under The MIT License,
	see the file LICENSE for details.
	*/

#include "WidgetRegistry.hpp"

#include <QuickGUI.h>

#include "HelpDefs.hpp"

namespace spelprog2 {
namespace gui {

void WidgetRegistry::add(const std::string & key, QuickGUI::Widget * w)
{
    assert(w);

    if (widgets_.find(key) != widgets_.end()) {
        throw Exception(0, "Widget " + key + " already exists", WHERE);
    }

    widgets_[key] = w;
}

QuickGUI::Widget & WidgetRegistry::get(const std::string & key)
{
    WidgetMap::const_iterator it = widgets_.find(key);
    if (it == widgets_.end()) {
        throw Exception(0, "Widget " + key + " not found", WHERE);
    }
    return *it->second;
}

void WidgetRegistry::destroy(const std::string & key)
{
    WidgetMap::iterator it = widgets_.find(key);
    if (it == widgets_.end()) {
        gamelog << "Warning: Didn't destroy widget " + key;
        return;
    }

    it->second->destroy();
    widgets_.erase(it);
}

}; //gui
}; //spelprog2
